/*
 * Teste.java
 *
 * Created on 05/06/2009, 10:48:03
 * Copyright Administrador
 */
package Soligee.view;

import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputSecret;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectedData;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.decorador.ValidaCamposObrigatorios;
import Soligee.model.vo.cadastros.UsuarioVO;
import javax.faces.FacesException;
import javax.faces.model.SelectItem;

public class CadastrarUsuario extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    private int __placeholder;
    private int controle = 0;
    private ValidaCamposObrigatorios valida = new ValidaCamposObrigatorios();
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {

    }
    private HtmlCommandButton btn_cadastrar = new HtmlCommandButton();

    public HtmlCommandButton getBtn_cadastrar() {
        return btn_cadastrar;
    }

    public void setBtn_cadastrar(HtmlCommandButton hcb) {
        this.btn_cadastrar = hcb;
    }
    private DefaultSelectedData selectOneMenu1Bean = new DefaultSelectedData();

    public DefaultSelectedData getSelectOneMenu1Bean() {
        return selectOneMenu1Bean;
    }

    public void setSelectOneMenu1Bean(DefaultSelectedData dsd) {
        this.selectOneMenu1Bean = dsd;
    }
    private DefaultSelectionItems selectOneMenu1DefaultItems = new DefaultSelectionItems();

    public DefaultSelectionItems getSelectOneMenu1DefaultItems() {
        return selectOneMenu1DefaultItems;
    }

    public void setSelectOneMenu1DefaultItems(DefaultSelectionItems dsi) {
        this.selectOneMenu1DefaultItems = dsi;
    }
    private HtmlSelectOneMenu nivelUsuario = new HtmlSelectOneMenu();

    public HtmlSelectOneMenu getNivelUsuario() {
        return nivelUsuario;
    }

    public void setNivelUsuario(HtmlSelectOneMenu hsom) {
        this.nivelUsuario = hsom;
    }
    private HtmlInputSecret campoSenha = new HtmlInputSecret();

    public HtmlInputSecret getCampoSenha() {
        return campoSenha;
    }

    public void setCampoSenha(HtmlInputSecret his) {
        this.campoSenha = his;
    }
    private HtmlInputText campoLogin = new HtmlInputText();

    public HtmlInputText getCampoLogin() {
        return campoLogin;
    }

    public void setCampoLogin(HtmlInputText hit) {
        this.campoLogin = hit;
    }
    private PopupBean panelPopup1Bean = new PopupBean();

    public PopupBean getPanelPopup1Bean() {
        return panelPopup1Bean;
    }

    public void setPanelPopup1Bean(PopupBean pb) {
        this.panelPopup1Bean = pb;
    }
    private PanelPopup panelPopup = new PanelPopup();

    public PanelPopup getPanelPopup() {
        return panelPopup;
    }

    public void setPanelPopup(PanelPopup pp) {
        this.panelPopup = pp;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private HtmlInputText campoIdUsuario = new HtmlInputText();

    public HtmlInputText getCampoIdUsuario() {
        return campoIdUsuario;
    }

    public void setCampoIdUsuario(HtmlInputText hit) {
        this.campoIdUsuario = hit;
    }
    private HtmlCommandButton btn_voltar = new HtmlCommandButton();

    public HtmlCommandButton getBtn_voltar() {
        return btn_voltar;
    }

    public void setBtn_voltar(HtmlCommandButton hcb) {
        this.btn_voltar = hcb;
    }
    private HtmlInputText inputText1 = new HtmlInputText();

    public HtmlInputText getInputText1() {
        return inputText1;
    }

    public void setInputText1(HtmlInputText hit) {
        this.inputText1 = hit;
    }
    private HtmlInputSecret campoConfirmaSenha = new HtmlInputSecret();
    private HtmlInputText campoNomeUsuario = new HtmlInputText();

    public HtmlInputText getCampoNomeUsuario() {
        return campoNomeUsuario;
    }

    public void setCampoNomeUsuario(HtmlInputText hit) {
        this.campoNomeUsuario = hit;
    }
    private HtmlInputSecret campoConfirmaSenha1 = new HtmlInputSecret();

    public HtmlInputSecret getCampoConfirmaSenha1() {
        return campoConfirmaSenha1;
    }

    public void setCampoConfirmaSenha1(HtmlInputSecret his) {
        this.campoConfirmaSenha1 = his;
    }
    private HtmlInputText campoCodigo = new HtmlInputText();

    public HtmlInputText getCampoCodigo() {
        return campoCodigo;
    }

    public void setCampoCodigo(HtmlInputText hit) {
        this.campoCodigo = hit;
    }

    // </editor-fold>
    public CadastrarUsuario() {
    }

    @Override
    public void init() {
        super.init();
        this.inicializarCampos();
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Teste Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }
        System.out.println("teste de cadastro");
    // </editor-fold>
    }

    public void limparCampos() {
        campoLogin.resetValue();
        nivelUsuario.resetValue();
        campoSenha.resetValue();
        campoConfirmaSenha1.resetValue();
        campoNomeUsuario.resetValue();
    }

    public void inicializarCampos() {

        campoLogin.setMaxlength(30);
        campoNomeUsuario.setMaxlength(50);
        campoSenha.setMaxlength(10);
        campoConfirmaSenha1.setMaxlength(10);
        panelPopup.setRendered(false);
        selectOneMenu1DefaultItems.clear();
        selectOneMenu1DefaultItems.add(new SelectItem(1, "Administrador"));
        selectOneMenu1DefaultItems.add(new SelectItem(2, "Gerente"));
        selectOneMenu1DefaultItems.add(new SelectItem(3, "Usuario"));
        //mostra o valor do id correto para inserção no banco
        SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();
        campoCodigo.setValue(facadeBusca.totalDeRegistros(UsuarioVO.class) + 1);
        limparCampos();

    }

    @Override
    public void preprocess() {
    }

    @Override
    public void prerender() {
    }

    @Override
    public void destroy() {
    }

    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    protected RequestBean1 getview$RequestBean1() {
        return (RequestBean1) getBean("view$RequestBean1");
    }

    protected SessionBean1 getview$SessionBean1() {
        return (SessionBean1) getBean("view$SessionBean1");
    }
    
    public String btn_cadastrar_action() {
           valida.verificaCampoVazio(campoNomeUsuario);
           valida.verificaCampoVazio(campoLogin);
           valida.verificaCampoVazio(campoSenha);
           
       if(valida.verificaCampoVazio(campoNomeUsuario)||valida.verificaCampoVazio(campoLogin)||valida.verificaCampoVazio(campoSenha))
           controlaPopup(true,"Campos Obrigatórios!",false,true,"","Ok");
       
            
           else if(!campoSenha.getValue().toString().equals(campoConfirmaSenha1.getValue().toString())){
            controlaPopup(true,"Confirmação de Senha Inválida!",false,true,"","Ok");
            campoSenha.resetValue();
            campoConfirmaSenha1.resetValue();
            }

       else{
       
       controlaPopup(true,"Confirma Cadastro de Usuário ?",true,true,"Sim","Não");
       }
        return null;
    }

    protected ApplicationBean1 getview$ApplicationBean1() {
        return (ApplicationBean1) getBean("view$ApplicationBean1");
    }

    protected CadastrarPessoaFisica getview$CadastrarPessoaFisica() {
        return (CadastrarPessoaFisica) getBean("view$CadastrarPessoaFisica");
    }

    protected BuscarPessoaFisica getview$BuscarPessoaFisica() {
        return (BuscarPessoaFisica) getBean("view$BuscarPessoaFisica");
    }

    protected CadastrarPedido getview$CadastrarPedido() {
        return (CadastrarPedido) getBean("view$CadastrarPedido");
    }

    protected Login getview$Login() {
        return (Login) getBean("view$Login");
    }

    protected BuscarRepresentante getview$BuscarRepresentante() {
        return (BuscarRepresentante) getBean("view$BuscarRepresentante");
    }

    protected BuscarVendedor getview$BuscarVendedor() {
        return (BuscarVendedor) getBean("view$BuscarVendedor");
    }

    protected CadastrarPessoaJuridica getview$CadastrarPessoaJuridica() {
        return (CadastrarPessoaJuridica) getBean("view$CadastrarPessoaJuridica");
    }

    protected BuscarNotaFiscal getview$BuscarNotaFiscal() {
        return (BuscarNotaFiscal) getBean("view$BuscarNotaFiscal");
    }

    protected CadastrarVendedor getview$CadastrarVendedor() {
        return (CadastrarVendedor) getBean("view$CadastrarVendedor");
    }

    public String btnPopUp2_action() {
        panelPopup.setRendered(false);
        return null;
    }


    //action - PopUp Cadastrar
    public String btnPopUp1_action() {
        if (controle == 0) {
            SessionFacadeCadastrarImplements cadastrar = new SessionFacadeCadastrarImplements();
            try {
                if (cadastrar.cadastrarUsuario(Integer.parseInt(campoCodigo.getValue().toString()), campoLogin.getValue().toString(), Integer.parseInt(nivelUsuario.getValue().toString()), campoSenha.getValue().toString(), campoNomeUsuario.getValue().toString())) {
                    controlaPopup(true, "Usuario Cadastrado com Sucesso!", false, true, "", "OK");
                    inicializarCampos();
                }
            } catch (Exception e) {
                controlaPopup(true, "Erro ao Cadastrar !!!", false, true, "", "OK");
                limparCampos();
            }

            return null;
        } else if (controle == 1) {
            panelPopup.setRendered(false);
            System.out.println("passou aki");
            return "voltar";
        }
        return null;

    }

    public void controlaPopup(boolean renderedPopup, String textoPoup, boolean btn1Rendered, boolean btn2Rendered,
            String textobtn1, String textobtn2) {

        panelPopup.setRendered(renderedPopup);
        textoPopUp.setValue(textoPoup);
        btnPopUp1.setValue(textobtn1);
        btnPopUp1.setRendered(btn1Rendered);
        btnPopUp2.setRendered(btn2Rendered);
        btnPopUp2.setValue(textobtn2);
    }

    //action btn_voltar
    public String btn_voltar_action() {
        controle = 1;
        controlaPopup(true, "Deseja Sair ?", true, true, "Sim", "Não");
        System.out.println(controle);
        return null;
    }

    public String panelLayoutLogo_action() {
        //return null means stay on the same page
        return "voltar";
    }

    //linkCancelar
    public String linkCancelar_action() {
        this.limparCampos();
        
        return null;
    }
}
    
